<?php get_header(); ?>

<?php if(have_posts()) : ?>

<h2 class="archive">Search result for '<?php echo strip_tags($_GET['s']) ?>'</h2>

<?php while(have_posts()) : the_post(); ?>
	<div class="post">
		<div class="post-gravatar"><?php echo get_avatar(get_the_author_email(), '50') ?></div>
		<div class="post-text">
			<h2 class="title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
			<?php the_excerpt(); ?>
			<div class="meta"><?php the_time('j F Y'); echo ' at '; the_time('H:i'); ?> - <a href="<?php the_permalink(); ?>#respond">Comments</a></div>
			<?php
				$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type = '' ORDER BY comment_date", $post->ID));
				if(count($comments) != 0) {
					$shown = array(0, 1);
					$i = 0;
					foreach($shown as $id) {
						if(isset($comments[$id])) {
							$i++;
							global $comment;
							$comment = $comments[$id];	?>
							<div class="index-comment">
								<div class="ic-avatar"><?php echo get_avatar(get_comment_author_email(), '37'); ?></div>
								<div class="ic-text">
									<div class="ic-meta"><?php echo get_comment_author_link(); ?> at <?php echo get_comment_date('H:i'); ?> on <?php echo get_comment_date('j F Y'); ?></div>
									<div><?php echo get_comment_excerpt() ?></div>
								</div>
							</div>
			<?php		}
					}
					if(count($comments) >= 3) { ?>
						<div class="index-comment"><a href="<?php the_permalink(); ?>#comments">Show <?php echo (count($comments) - $i) ?> more comments..</a></div>
			<?php	}
				}
			?>			
		</div>
	</div>
<?php endwhile; ?>

	<div class="navigation">
		<div class="next"><?php next_posts_link('Show Older Posts..') ?></div>
		<div class="prev"><?php previous_posts_link('Show Newer Posts..') ?></div>
	</div>
<?php else : ?>
	There are no results for <?php echo strip_tags($_GET['s']) ?>. Make sure all words are spelt correctly.
<?php endif; ?>
<?php get_footer(); ?>